package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageResult pageSelect(Integer page, Integer pageSize, Long categoryId, String name, Integer status) {

        PageResult pageResult = new PageResult();

        PageHelper.startPage(page, pageSize);

        Page<SetmealVO> setmeals = setmealMapper.findSetmealByCategoryIdAndNameAndStatus(categoryId, name, status);

        pageResult.setTotal(setmeals.getTotal());
        pageResult.setRecords(setmeals.getResult());

        return pageResult;
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void insert(SetmealDTO setmealDTO) {
        //1.新增套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmealMapper.insert(setmeal);

        //2.查询菜品
        List<SetmealDish> dishes1 = setmealDTO.getSetmealDishes();
        int index = 0;
        for(SetmealDish dish : dishes1){
            Dish dishIndex = dishMapper.findDishByDishId(dish.getDishId());
            log.info("菜品信息为：{}", dishIndex);
            BeanUtils.copyProperties(dishIndex, dishes1.get(index));
            dishes1.get(index).setSetmealId(setmeal.getId());
            index++;
        }

        //3.批量新增套餐菜品关系
        setmealDishMapper.insertBatch(dishes1);

    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void numDelete(List<Long> ids) {
       if(!(ids.size() > 0)){
           throw new BaseException("请选择要删除的套餐");
       }
       for(Long id : ids){
           Setmeal setmeal = setmealMapper.findDishBySetmealId(id);
           if(setmeal.getStatus() == 1){
               throw new BaseException(MessageConstant.SETMEAL_ON_SALE);
           }
       }
       //删除套餐
       setmealMapper.deleteBatch(ids);
       //删除套餐对应的套餐菜品关系表
        setmealDishMapper.deleteBySetmealId(ids);
    }

    /**
     * 起售停售套餐
     * @param status
     * @param id
     */
    @Override
    public void updateStatusBySetmealId(Integer status, Long id) {
        setmealMapper.updateStatusBySetmealId(status, id);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        //1，查询套餐
       Setmeal setmeal = setmealMapper.findSetmealById(id);
        BeanUtils.copyProperties(setmeal, setmealVO);
        //获取分类名称
        setmealVO.setCategoryName(categoryMapper.findCategoryNameByCategoryId(setmeal.getCategoryId()));

       //2.查询套餐和菜品关系得到菜品id
       Long dishId = setmealDishMapper.findDishIdBySetmealId(id);

       //3.查询菜品并copy数据到菜品VO中
        List<SetmealDish> setmealDishes = dishMapper.findDishsByDishId(dishId);

        //4.封装套餐VO并返回
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }


    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {

        //判断值是否输入正确

        //1 修改套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmealMapper.update(setmeal);

        //2 修改套餐菜品关系表
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if(setmealDishList != null && setmealDishList.size() > 0){
            //全删
            setmealDishMapper.deleteByDishId(setmealDTO.getId());
            //添加
            int index = 0;
            for(SetmealDish setmealDish : setmealDishList){
                Dish dish = dishMapper.findDishByDishId(setmealDish.getDishId());
                BeanUtils.copyProperties(dish, setmealDish);
                setmealDish.setSetmealId(setmealDTO.getId());
            }
            setmealDishMapper.insertBatch(setmealDishList);

        }
    }
}
